<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span style="position: relative; top: 10px">
          <i class="el-icon-search"></i>
          搜索
        </span>
        <el-button-group style="float: right; padding: 3px 0">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-search"
            @click="btnSearch"
            >搜索</el-button
          >
          <el-button
            size="small"
            icon="el-icon-refresh"
            type="primary"
            @click="btnReset"
            >重置</el-button
          >
        </el-button-group>
      </div>
      <el-form :model="searchForm" :inline="true" ref="productTypeFormRef">
        <el-row>
          <el-col :span="colSize">
            <el-form-item label="订单编号" prop="id">
              <el-input
                v-model="searchForm.id"
                placeholder="订单编号"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="colSize">
            <el-form-item label="订单状态" prop="status">
              <el-select
                v-model="searchForm.status"
                filterable
                placeholder="请选择"
              >
                <el-option
                  v-for="item in statusList"
                  :key="item.key"
                  :label="item.value"
                  :value="item.key"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="colSize">
            <el-form-item label="提货方式" prop="pickupWay">
              <el-select
                      v-model="searchForm.pickupWay"
                      filterable
                      placeholder="请选择"
              >
                <el-option
                        v-for="item in pickList"
                        :key="item.key"
                        :label="item.value"
                        :value="item.key"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="data-card">
      <div slot="header" class="clearfix">
        <span style="position: relative; top: 7px">
          <i class="el-icon-tickets"></i>
          订单列表
        </span>
        <div style="float: right; padding: 3px 0">
<!--          <el-button-->
<!--            size="small"-->
<!--            type="warning"-->
<!--            icon="el-icon-plus"-->
<!--            @click="btnAdd"-->
<!--            >添加</el-button-->
<!--          >-->
          <!--          <el-button-->
          <!--            size="small"-->
          <!--            type="danger"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="btnDelete"-->
          <!--            >批量删除</el-button-->
          <!--          >-->
        </div>
      </div>
      <el-table fit :data="orderList" stripe style="width: 100%">
        <el-table-column align="center" type="selection" width="55">
        </el-table-column>
        <el-table-column align="center" prop="id" label="订单编号">
        </el-table-column>
        <el-table-column align="center" prop="consignee" label="收货人">
        </el-table-column>
        <el-table-column align="center" label="收货地址" prop="address">
        </el-table-column>
        <el-table-column align="center" label="联系方式" prop="contact">
        </el-table-column>
        <el-table-column align="center" label="提货方式" prop="pickupWay">
          <template slot-scope="scope">
            <p>{{scope.row.pickupWay | pickupWayFilter}}</p>
          </template>
        </el-table-column>
        <el-table-column align="center" label="支付方式" prop="payWay">
        </el-table-column>
        <el-table-column align="center" label="订单金额" prop="originalPrice">
        </el-table-column>
        <el-table-column align="center" label="下单时间" prop="createTime" :formatter="dateFormat">
        </el-table-column>
        <el-table-column align="center" label="状态" prop="status">
          <template slot-scope="scope">
            <p>{{scope.row.status | statusFilter}}</p>
          </template>
        </el-table-column>
        <el-table-column width="300" align="center" label="操作">
          <template slot-scope="scope">
            <p>
              <el-button
                size="mini"
                icon="el-icon-view"
                @click="btnView(scope.row)"
                >查看</el-button
              >
              <el-button
                v-if="scope.row.pickupWay=='invite' && scope.row.status=='PAID'"
                size="mini"
                type="warning"
                icon="el-icon-box"
                @click="btnUpdateStatus('stockup', scope.row)"
                >备货</el-button
              >
              <el-button
                v-if="scope.row.pickupWay=='deliver' && scope.row.status=='PAID'"
                size="mini"
                icon="el-icon-truck"
                type="success"
                @click="btnUpdateStatus('send', scope.row)"
                >送出</el-button
              >
            </p>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        style="margin-top: 20px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[10, 20, 30]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
  import {api_orderList, api_updateOrderStatus} from "../../api/order";
export default {
  data() {
    return {
      colSize: 6,
      orderList: [],
      statusList: [
        {
          key:"CANCEL",
          value:"已取消"
        },
        {
          key:"PAID",
          value:"已支付"
        },
        {
          key:"STOCKUP",
          value:"已备货"
        },
        {
          key:"SEND",
          value:"已送出"
        },
        {
          key:"FINISH",
          value:"已完成"
        },
      ],
      pickList:[
        {
          key:"deliver",
          value:"配送"
        },
        {
          key:"invite",
          value:"自提"
        },
      ],
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        status: null,
        pickupWay:null,
      },
      page: {
        pageNum: 1,
        pageSize: 10,
        totalPage: 3,
        total: 11,
      },
    }
  },
  created() {
    this.btnSearch()
  },
  filters: {
    pickupWayFilter(value){
      let text = '自提'
      if(value==='deliver'){
        text = '配送'
      }
      return text;
    },
    statusFilter(value) {
      //上架 售空 下架
      let text = ''
      switch(value){
        case 'CANCEL':
          text = '已取消';
          break;
        case 'PAID':
          text = '已支付';
          break;
        case 'STOCKUP':
          text = '已备货';
          break;
        case 'SEND':
          text = '已送出';
          break;
        case 'FINISH':
          text = '已完成';
          break;
        default:
          break;
      }
      return text
    },
  },
  methods: {
    dateFormat(row, column, cellValue) {
      if(cellValue==null || cellValue=="") return "";
      let date = new Date(cellValue);
      let Y = date.getFullYear() + '-';
      let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) + '-' : date.getMonth() + 1 + '-';
      let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
      let h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
      let m = date.getMinutes()  < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
      let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
      return Y + M + D + h + m + s ;
    },
    btnSearch() {
      this.handleOrderList()
    },
    btnReset() {
      this.$refs.productTypeFormRef.resetFields()
    },
    btnAdd() {
      this.$router.push('/order/OrderView')
    },
    btnView(row) {
      this.$router.push({
        path: '/order/OrderView',
        query: {
          id: row.id,
          handleType: 'view',
        },
      })
    },
    btnEdit(row) {
      this.$router.push({
        path: '/order/OrderView',
        query: {
          id: row.id,
          handleType: 'edit',
        },
      })
    },
    async btnDelete(row) {
      this.$confirm('此操作将删除选中的数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          //获取选中行id集合
          this.handleDelete(row.id)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消',
          })
        })
    },
    async btnUpdateStatus(type,row){
      let status
      if (type === 'stockup') {
        //备货
        status = "STOCKUP"
      } else if (type === 'send') {
        //送出
        status = "SEND"
      }
      let ids = []
      ids.push(row.id)
      const param = {
        ids: JSON.stringify(ids),
        status: status,
      }
      const { data: res } = await api_updateOrderStatus(param)
      if (!res.success) {
        return this.$message.error(res.msg)
      }
      this.$message({
        message: res.msg,
        type: 'success',
      })
      this.btnSearch()
    },
    handleDelete(id) {
      let params = new URLSearchParams()
      params.append('id', id)
      // api_deleteProductType(params).then((response) => {
      //     const res = response.data
      //     if (!res.success) {
      //         this.$message.error(res.msg)
      //         return
      //     }
      //     this.$message({
      //         type: 'success',
      //         message: res.msg,
      //     })
      //     this.btnSearch()
      // })
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.btnSearch()
    },
    handleCurrentChange(val) {
      this.searchForm.pageNum = val
      this.btnSearch()
    },
    //获取订单列表
    async handleOrderList() {
      const param = {
          page: this.searchForm.pageNum,
          limit: this.searchForm.pageSize,
          bean: JSON.stringify(this.searchForm),
      }
      const { data: res } = await api_orderList(param)
      this.orderList = res.data.data
      this.page.total = res.data.total
    },
  },
}
</script>

<style scoped>
.clearfix:before,
.clearfix:after {
  display: table;
  content: '';
}
.clearfix:after {
  clear: both;
}
.box-card {
  width: 100%;
  margin: auto;
}
.data-card {
  margin-top: 10px;
}

.el-date-picker {
  float: left;
}
</style>
